package com.hycloud.cloudstack.resource;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hycloud.cloudstack.common.CloudstackApi;
import com.hycloud.cloudstack.common.ResponseData;
import com.hycloud.cloudstack.common.WebUtils;
import com.hycloud.cloudstack.model.Template;
import com.hycloud.cloudstack.service.TemplateService;

@RestController
@RequestMapping("/deploy")
public class DeployVm {
	@Autowired
  private TemplateService templateService;
	@Autowired
	private CloudstackApi cloudstackApi;
	
	@RequestMapping("/templates")
	public ResponseEntity<ResponseData> queryAllTemplate(){
		try {
			int count  = templateService.templateCount();
			List<Template> list = templateService.queryAllTemplate();
			return ResponseEntity.status(HttpStatus.OK).body(new ResponseData(count, list));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 列出所有的网路
	 */
	@RequestMapping("/listNetworks")
	public ResponseEntity<String> listNetworks(HttpServletRequest request){
		Map<String, String> params = WebUtils.getParameterAsMap(request);
		try {
			String result = cloudstackApi.api(params);
			return ResponseEntity.status(HttpStatus.OK).body(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
}
